package com.xgpro.mobile.message.controller;

import com.xgpro.common.results.PageResult;
import com.xgpro.common.results.ResponseResult;
import com.xgpro.common.utils.DDMsgUtil;
import com.xgpro.common.utils.SendMsgUtil;
import com.xgpro.domain.messagemanage.entity.Message;
import com.xgpro.domain.rolemanage.dto.UserDTO;
import com.xgpro.domain.talentintroduction.entity.User;
import com.xgpro.mobile.message.service.MessageService;
import com.xgpro.pc.rolemanage.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @program: xgpro__dev
 * @description: 消息管理controller
 * @author: likai
 * @create: 2019-09-27 02:45
 **/

@CrossOrigin(allowCredentials="true")
@RestController
public class MessageController {
    @Resource
    private MessageService messageService;

    @Resource
    private UserService userService;

    @Resource
    private SendMsgUtil sendMsgUtil;

    @Resource
    private DDMsgUtil ddMsgUtil;

    /**
     * 分页获取用户消息列表
     * @param pageNum
     * @param pageSize
     * @param isRead
     * @return
     */
    @RequestMapping("message_list/v1")
    public ResponseResult queryMessageList(int pageNum, int pageSize, String isRead, HttpServletRequest request){
        Integer id = (int)request.getSession().getAttribute("id");
        List<Message> messages = messageService.queryList(pageNum, pageSize, isRead, id);
        //获取用户的消息总条数
        long count = messageService.getCount(id, isRead);
        PageResult pageResult = new PageResult(count,messages,pageNum);

        return ResponseResult.ok(pageResult);
    }

    /**
     * 获取用户最新的一条消息
     * @param isRead
     * @return
     */
    @RequestMapping("message/v1")
    public ResponseResult queryOneNewMessage(String isRead,HttpServletRequest request){
        Integer id = (int)request.getSession().getAttribute("id");
        Message message = messageService.queryOneNewMessage(isRead, id);
        return ResponseResult.ok(message);
    }

    /**
     * 获取当前用户的消息条数
     */
    @GetMapping("/count")
    public ResponseResult getCount(String isRead,HttpServletRequest request){
        Integer id = (int)request.getSession().getAttribute("id");
        int cnt = messageService.getCount(id,isRead);
        return ResponseResult.ok(cnt);
    }

    /**
     * 获取指定消息的详情
     */
    @GetMapping("/detail/{msgId}")
    public ResponseResult findOne(@PathVariable("msgId") Integer msgId){
        Message msg = messageService.findOne(msgId);
        return ResponseResult.ok(msg);
    }

    /**
     * 改变消息的状态为已读
     * @param msgId
     * @return
     */
    @GetMapping("/update/{msgId}")
    public ResponseResult updateActive(@PathVariable Integer msgId,HttpServletRequest request){

        Integer id = (int)request.getSession().getAttribute("id");
        int cnt = messageService.updateActive(msgId,id);
        //消息状态改变后，返回消息的主体
        Message one = messageService.findOne(msgId);
        return ResponseResult.ok(one);
    }

    /**
     * 当前用户全部消息掷为已读
     * @return
     */
    @GetMapping("/readAll")
    public ResponseResult allRead(HttpServletRequest request){
        Integer id = (int)request.getSession().getAttribute("id");
        int cnt = messageService.updateActive(null,id);

        return ResponseResult.ok();

    }

    @GetMapping("/hello")
    public ResponseResult testMessageSend() throws Exception {

        PageResult pageResult = userService.listUser(1, 100, new HashMap<>());
        List<UserDTO> rows = pageResult.getRows();
        List<Integer> list = new ArrayList<>();
        for (UserDTO user : rows) {
            list.add(user.getId());

        }
        sendMsgUtil.sendMsg(0,"测试消息推送标题","测试消息推送内容",list);
        String s = userService.selectAllUserId(list);
        System.out.println(s);
        ddMsgUtil.sendMsg(s,"测试消息推送内容");

        return ResponseResult.ok();
    }
}
